package com.tte.common.thread;

/**
 * ThreadLocaDemo
 * @Author: DongWH
 */
public class ThreadLocaDemo {
    private static ThreadLocal<String> t1 = new ThreadLocal<String>();

    static void print(String str) {
        //打印当前线程中本地内存中本地变量的值
        System.out.println(str + " :" + t1.get());
        //清除本地内存中的本地变量
        t1.remove();
    }
    public static void main(String[] args) throws InterruptedException {

        t1.set("主线程");

        new Thread(new Runnable() {
            @Override
            public void run() {
                ThreadLocaDemo.t1.set("local_A");
                print("A");
                //打印本地变量
                System.out.println("after remove : " + t1.get());
                t1.remove();
            }
        },"A").start();

        Thread.sleep(1000);

        new Thread(new Runnable() {
            @Override
            public void run() {
                ThreadLocaDemo.t1.set("local_B");
                print("B");
                System.out.println("after remove : " + t1.get());
                t1.remove();

            }
        },"B").start();

        System.out.println("主线程：" + t1.get());
        t1.remove();

    }
}
